package com.my.ext.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.my.ext.entity.Admin;
import com.my.ext.entity.Org;
import com.my.ext.service.impl.OrgService;
import com.my.ext.utils.ResultObject;

@RestController
@RequestMapping(value = {"/api/org","/org"}, method = {RequestMethod.GET, RequestMethod.POST})
public class OrgController {

	@Autowired
	OrgService service;
	
	@RequestMapping("/queryOrgs")
	public ResultObject queryOrgs(int page, int size) {
		PageInfo info = service.findAllOrgs(page, size);
		ResultObject result = new ResultObject();
		result.setStatus(1);
		result.setData(info);
		return result;
	}
	
	@RequestMapping("/addOrg")
	@RequiresAuthentication
	public ResultObject addOrg(Org org, Admin admin) {
		service.saveOrg(org, admin);
		ResultObject obj = new ResultObject();
		obj.setStatus(1);
		obj.setData(1);
		return obj;
	}
	
	@RequiresAuthentication
	@RequestMapping("/queryOrg")
	public ResultObject queryMyOrg() {
		Subject sub = SecurityUtils.getSubject();
		Org org = service.findOrgByAdmin((Admin)sub.getPrincipal());
		ResultObject obj = new ResultObject();
		obj.setStatus(1);
		obj.setData(org);
		return obj;
	}
	
}
